const app = getApp();
const skinBehavior = require('../../../utils/themeSkin/skinBehavior');
Page({
  behaviors: [skinBehavior],
  /**
   * 页面的初始数据
   */
  data: {
     audio_id:'',
     book_id:'',
     activeIndex:1,
     currentSrc:'',
     audio_title:'',
     content:'',
     image_url:'',
     isLoadMore:true,
     page:1,
     last_page:0,
     dataList:[],
     emptyImage: app.globalData.imgPath + "image.png",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      book_id:options.id
    })
    this.getContent()
    this.getDirectoryData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if(this.data.activeIndex == 1){
      return
    }
    if (this.data.isLoadMore) {
      this._getMoreData();
    } else {
      console.log('没有更多数据');
      wx.showToast({
        title: '没有更多数据',
        icon:'none',
        duration:1000
      });
    } 
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  //改变tap状态
  changeTap(e){
    let index = e.currentTarget.dataset['index']
    
    this.setData({
      activeIndex:index
    })
  },
  tapSrc(e){
    // this.setData({
    //   currentSrc:'http://music.163.com/song/media/outer/url?id=447925558.mp3'
    // })
    this.getContent(e.currentTarget.dataset.id)
  },
  getContent(audio_id){
    let json = {
      book_id:this.data.book_id
    }
    if(audio_id){
      json = {
        book_id:this.data.book_id,
        audio_id
      };
    }
    let urlStr = app.getNetAddresss("plugin.audio-book.frontend.audio.get-detail");
    app._postNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          this.setData({
            currentSrc:res.data.data.audio_url,
            audio_title:res.data.data.audio_title,
            content:res.data.data.content.replace(/<img/g,"<img style='max-width:100%;height:auto;'").replace(/<video/g, "<video style='width:100%;height:auto;'"),
            image_url:res.data.data.image_url,
            activeIndex:1,
            audio_id:res.data.data.id
          });
          this.playLog()
          wx.pageScrollTo({
            duration: 0,
          })
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1000,
          });
        }
      },
      fail: (res) => {
        console.log(res);
      },
    });    
  },
  getDirectoryData(){
    let json = {
      book_id:this.data.book_id
    }
    let urlStr = app.getNetAddresss("plugin.audio-book.frontend.audio.get-list");
    app._postNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          let _data = res.data.data
          this.setData({
            page:res.data.data.current_page,
            last_page:res.data.data.last_page,
            dataList:res.data.data.data
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1000,
          });
        }
      },
      fail: (res) => {
        console.log(res);
      },
    });   
  },
  _getMoreData(){
    var that = this;
    let urlStr = app.getNetAddresss("plugin.audio-book.frontend.audio.get-list");
    if (this.data.page >= this.data.last_page) {
      that.setData({
        isLoadMore: false
      });
      return;
    } else {
      that.setData({
        page: this.data.page + 1,
      });
      app._getNetWork({
        url: urlStr,
        data: {
          page: that.data.page,
          book_id:this.data.book_id
        },
        success: function (res) {
          if (res.data.result == 1) {
            let _data = res.data.data.data.data;
            that.setData({
              isLoadMore: true,
              dataList: that.data.dataList.concat(_data)
            });
          } else {
            that.setData({
              page: that.data.page - 1,
              isLoadMore: false
            });
            return;
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }   
  },
  //记录播发次数
  playLog(){
    let json = {
      book_id:this.data.book_id,
      audio_id:this.data.audio_id
    }
    let urlStr = app.getNetAddresss("plugin.audio-book.frontend.audio.memory-play-log");
    app._postNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          console.log(res.msg);
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1000,
          });
        }
      },
      fail: (res) => {
        console.log(res);
      },
    });   
  }
})